2020-2021 Sunseeker Telemetry and Lighting System
adc12_a_ex8_tempSensor.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 #include "driverlib.h"
33 
34 //******************************************************************************
68 //******************************************************************************
69 
70 long temp;
71 volatile long IntDegF;
72 volatile long IntDegC;
73 
74 void main (void)
75 {
76  //Stop Watchdog Timer
77  WDT_A_hold(WDT_A_BASE);
78 
79  //Initialize the ADC12_A Module
80  /*
81  * Base Address of ADC12_A Module
82  * Use internal ADC12_A bit as sample/hold signal to start conversion
83  * USE MODOSC 5MHZ Digital Oscillator as clock source
84  * Use default clock divider of 1
85  */
86  ADC12_A_init(ADC12_A_BASE,
87  ADC12_A_SAMPLEHOLDSOURCE_SC,
88  ADC12_A_CLOCKSOURCE_ADC12OSC,
89  ADC12_A_CLOCKDIVIDER_1);
90 
91  ADC12_A_enable(ADC12_A_BASE);
92 
93  ADC12_A_enable(ADC12_A_BASE);
94 
95  /*
96  * Base Address of ADC12_A Module
97  * For memory buffers 0-7 sample/hold for 768 clock cycles
98  * For memory buffers 8-15 sample/hold for 4 clock cycles (default)
99  * Disable Multiple Sampling
100  *
101  * Note: ADC12_A may have a minimum sample rate of 30us or 100us.
102  * This example uses a sample time of ~154us to ensure it works.
103  * 256 cycles (51.2us) or 512 cycles (100.4us) may be used, but
104  * resulting readings may be less accurate.
105  */
106  ADC12_A_setupSamplingTimer(ADC12_A_BASE,
107  ADC12_A_CYCLEHOLD_768_CYCLES,
108  ADC12_A_CYCLEHOLD_4_CYCLES,
109  ADC12_A_MULTIPLESAMPLESDISABLE);
110 
111  //Configure Memory Buffer
112  /*
113  * Base Addres of ADC12_A Module
114  * Configure memory buffer 0
115  * Map temp sensor to memory buffer 0
116  * Vref+ = Vref+ (int)
117  * Vref- = AVss
118  * Memory buffer 0 is not the end of a sequence
119  */
120  ADC12_A_configureMemoryParam param = {0};
121  param.memoryBufferControlIndex = ADC12_A_MEMORY_0;
122  param.inputSourceSelect = ADC12_A_INPUT_TEMPSENSOR;
123  param.positiveRefVoltageSourceSelect = ADC12_A_VREFPOS_INT;
124  param.negativeRefVoltageSourceSelect = ADC12_A_VREFNEG_AVSS;
125  param.endOfSequence = ADC12_A_NOTENDOFSEQUENCE;
126  ADC12_A_configureMemory(ADC12_A_BASE ,&param);
127 
128  //Enable memory buffer 0 interrupt
129  ADC12_A_clearInterrupt(ADC12_A_BASE,
130  ADC12IFG0);
131  ADC12_A_enableInterrupt(ADC12_A_BASE,
132  ADC12IE0);
133 
134  //Configure internal reference
135  //If ref generator busy, WAIT
136  while ( REF_ACTIVE == Ref_isRefGenBusy(REF_BASE) ) ;
137  //Select internal ref = 1.5V
138  Ref_setReferenceVoltage(REF_BASE,
139  REF_VREF1_5V);
140  //Internal Reference ON
141  Ref_enableReferenceVoltage(REF_BASE);
142 
143  //Delay (~75us) for Ref to settle
144  __delay_cycles(75);
145 
146  while (1)
147  {
148  //Enable/Start first sampling and conversion cycle
149  /*
150  * Base address of ADC12_A Module
151  * Start the conversion into memory buffer 0
152  * Use the repeated sequence of channels
153  */
154  ADC12_A_startConversion(ADC12_A_BASE,
155  ADC12_A_MEMORY_0,
156  ADC12_A_SEQOFCHANNELS);
157 
158  //LPM4 with interrupts enabled
159  __bis_SR_register(LPM4_bits + GIE);
160  __no_operation();
161 
162  //Temperature in Celsius
163  //((A10/4096*1500mV) - 680mV)*(1/2.25mV) = (A10/4096*667) - 302
164  //= (A10 - 1855) * (667 / 4096)
165  IntDegC = ((temp - 1855) * 667) / 4096;
166 
167  //Temperature in Fahrenheit
168  //((A10/4096*1500mV) - 640mV)*(1/1.25mV) = (A10/4096*1200) - 512
169  //= (A10 - 1748) * (1200 / 4096)
170  IntDegF = ((temp - 1748) * 1200) / 4096;
171 
172  //SET BREAKPOINT HERE and watch IntDegC and IntDegF
173  __no_operation();
174  }
175 }
176 
177 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
178 #pragma vector=ADC12_VECTOR
179 __interrupt
180 #elif defined(__GNUC__)
181 __attribute__((interrupt(ADC12_VECTOR)))
182 #endif
183 void ADC12ISR (void)
184 {
185  switch (__even_in_range(ADC12IV,34)){
186  case 0: break; //Vector 0: No interrupt
187  case 2: break; //Vector 2: ADC overflow
188  case 4: break; //Vector 4: ADC timing overflow
189  case 6: //Vector 6: ADC12IFG0
190  //Move results, IFG is cleared
191  temp = ADC12_A_getResults(ADC12_A_BASE,
192  ADC12_A_MEMORY_0);
193 
194  //Exit active CPU
195  __bic_SR_register_on_exit(LPM4_bits);
196  break;
197  case 8: break; //Vector 8: ADC12IFG1
198  case 10: break; //Vector 10: ADC12IFG2
199  case 12: break; //Vector 12: ADC12IFG3
200  case 14: break; //Vector 14: ADC12IFG4
201  case 16: break; //Vector 16: ADC12IFG5
202  case 18: break; //Vector 18: ADC12IFG6
203  case 20: break; //Vector 20: ADC12IFG7
204  case 22: break; //Vector 22: ADC12IFG8
205  case 24: break; //Vector 24: ADC12IFG9
206  case 26: break; //Vector 26: ADC12IFG10
207  case 28: break; //Vector 28: ADC12IFG11
208  case 30: break; //Vector 30: ADC12IFG12
209  case 32: break; //Vector 32: ADC12IFG13
210  case 34: break; //Vector 34: ADC12IFG14
211  default: break;
212  }
213 }
214 
MPU_initThreeSegmentsParam param
void main(void)
long temp
volatile long IntDegC
volatile long IntDegF
void ADC12ISR(void)
__no_operation()
__bic_SR_register_on_exit(LPM3_bits|GIE)
__delay_cycles(500000)